
# _*_ coding:utf-8 _*_
import os
import xml.etree.ElementTree as ET


dirpath = r'/media/hjh/workdir/0_Deep_Learning/datasets/ciwa_beimian_VOC2007/Annotations'  # 原来存放xml文件的目录
# dirpath = r'H:\磁瓦\测试数据集\端面\划分数据集\train/'

# newdir = 'G:\研究生文件夹（深大）\机器学习\磁瓦\磁瓦(标注数据)\dataset\labels1'  # 修改label后形成的txt目录

# if not os.path.exists(newdir):
#     os.makedirs(newdir)

# write_file_name = 'valid.txt'
# write_file = open(write_file_name, "w")

qx_class = []
qx_num = {}

i = 0
names = []
FileNames = os.listdir(dirpath)
max_w = 1e4
max_h = 1e4
for fp in FileNames:
    if fp.endswith('.xml'):
        # i = i + 1
        root = ET.parse(os.path.join(dirpath, fp)).getroot()

        xmin, ymin, xmax, ymax = 0, 0, 0, 0
        sz = root.find('size')
        width = float(sz[0].text)
        height = float(sz[1].text)

        # if width > max_w:
        #     max_w = width
        # if height > max_h:
        #     max_h = height

        filename = root.find('filename').text

        #检查xml文件标注是否为空
        a = root.findall('object')
        # if a == []:
        #     i = i + 1
        #     print(fp)
            # write_file.write(fp + '\n')

        for child in root.findall('object'):  # 找到图片中的所有框

            # w = int(child[4][2].text) - int(child[4][0].text)
            # h = int(child[4][3].text) - int(child[4][1].text)
            # if w < max_w:
            #     if w == 3:
            #         print(fp)
            #     max_w = w
            # if h < max_h:
            #     if h == 3:
            #         print(fp)
            #     max_h = h

            name = child.findtext('name')
            if name == 'yichang':
                print(fp)
            box = child.findall('robndbox')
            if name not in qx_class:
                qx_class.append(name)
                qx_num[name] = 1
    
            else:
                qx_num[name] += 1


print('缺陷类别有：', qx_class)
print('各缺陷个数为：', qx_num)
# print(qx_class)
# print("max_h: ", max_h)
# print("max_w: ", max_w)